package com.example.gesture

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.gesture.ui.theme.JetpackComposeBasicTheme

/**
 * @author huangzehui
 * @date 12/5/2024 上午11:45 周日
 * @description
 *
 * 滚动修饰符
 *
 * https://www.bilibili.com/video/BV1ob4y1a7ad?p=62&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */
class MainActivity01 : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeBasicTheme {
                ScrollableSample()
            }
        }
    }

    /**
     * 滚动修饰符
     */
    @Composable
    fun ScrollBoxes() {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.LightGray)
                .verticalScroll(rememberScrollState())
        ) {
            repeat(100) {
                Text(
                    text = "Item ${it}",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 10.dp, vertical = 10.dp)
                )
            }
        }
    }


    /**
     * 借助 ScrollState，您可以更改滚动位置或获取当前状态
     */
    @Composable
    fun ScrollBoxesSmooth() {
        val state = rememberScrollState()

        LaunchedEffect(key1 = Unit) {
            state.animateScrollTo(1000)
        }

        Column(
            modifier = Modifier
                .background(Color.LightGray)
                .fillMaxSize()
                .verticalScroll(state)
        ) {
            repeat(100) {
                Text(
                    text = "item ${it}",
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 10.dp)
                )
            }
        }
    }

    /**
     * https://www.bilibili.com/video/BV1ob4y1a7ad?p=63&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
     *
     * 可滚动的修饰符
     * scrollable修饰符与滚动修饰符不同，区别在于scrollable可检测滚动手势，但不会偏移其内容。
     *
     */
    @Composable
    fun ScrollableSample() {

        var offset by remember {
            mutableFloatStateOf(0f)
        }

        val state = rememberScrollableState { delta ->
            offset += delta
            delta
        }

        Box(
            modifier = Modifier
                .size(150.dp)
                .scrollable(
                    orientation = Orientation.Vertical,
                    state = state
                )
                .background(Color.LightGray),
            contentAlignment = Alignment.Center
        ) {
            Text(text = offset.toString())
        }
    }

}